package Leetcode.array;

public class Rotate {
    public void rotate1(int[] nums, int k) {
        int n=nums.length;
        k=k%n;
        if(k==0||n<2) {
            return;
        }
        int i=0;
        int j=0;
        int start=0;
        int temp=nums[0];
         while( j<n){
             int f=nums[(i+k)%n];
             nums[(i+k)%n]=temp;

             i=(i+k)%n;
             if(i==start){
                 start++;
                 i=start;
                 temp=nums[start];
             }else {
                 temp=f;
             }
             j++;
         }

    }
    public void rotate2(int[] nums, int k) {
        int n=nums.length;
            k=k%n;
            reverse(nums,0,n-k-1);
           reverse(nums,n-k,n-1);
           reverse(nums,0,n-1);

    }

    public  void reverse(int[] num,int m,int n){
        for(int i=m;i<(m+n+1)/2;i++){
            int temp=num[i];
            num[i]=num[n+m-i];
            num[n+m-i]=temp;
        }

    //    for (int j=m;j<=n;j++)
      //      System.out.print(num[j]+" ");

      //  System.out.println("===============");
    }


    public static void main(String[] args) {
         int[] a={1,2,3,4,5,6,7};
         int k=3;

         Rotate rotate=new Rotate();
         rotate.rotate2(a,k);

         for(int i=0;i<a.length;i++){
             System.out.println(a[i]);
         }

    }
}
